/*****************************************************************************
作者：armwind.chen@foxmail.com
时间：2019.10.1
介绍：本事例仅用于学习，代码中包含了主从模式，具体是主还是从需要打开对应的话宏。如何操作在下面
     77行代码已经说过。
宝贝链接：https://item.taobao.com/item.htm?spm=a2oq0.12575281.0.0.50111debxjgygH&ft=t&id=626242045207

*****************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "relay.h"
#include "main.h"
#include "led.h"
#include "usart.h"
#include "common.h"

/** @addtogroup STM32F0xx_StdPeriph_Templates
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
    CanTxMsg TxMessage;

    TxMessage.StdId=0x111;       // 设定标准标识符（11位，扩展的为29位）
    TxMessage.RTR=CAN_RTR_DATA;  // 传输消息的帧类型为数据帧（还有远程帧）
    TxMessage.IDE=CAN_ID_STD;    // 消息标志符实验标准标识符
    TxMessage.DLC=2;             // 发送两帧，一帧8位
    TxMessage.Data[0]=0xCA;      // 第一帧数据
    TxMessage.Data[1]=0xFE;      // 第二帧数据

    /* Enable GPIO clock */
    delay_init();
    //usart_config(1,9600);
    usart_config(2,115200);
    led_init();
    relay_init();
    //CAN_ClearITPendingBit(CAN,CAN_IT_TME);
    SYS_DEBUG(("hello world1"));
    //__enable_irq();
    //can_init(50000); //250k
    can_init(125000); //250k
    //printf("hello world");
  /* Infinite loop */
    SYS_DEBUG(("hello world2"));
    __NVIC_EnableIRQ(CEC_CAN_IRQn);
    CAN_ClearITPendingBit(CAN,CAN_IT_TME);

    CAN->IER &= ~(1<<3);
    uint8_t mbox;
    while (1) {
            //LED = LED_ON;
            GPIO_SetBits (GPIOA, GPIO_Pin_4);
            //relay_ctrl(LEFT_RELAY, RELAY_ON);
            //relay_ctrl(RIGHT_RELAY, RELAY_OFF);
            delay_ms (1000);
            //LED = LED_OFF;
            GPIO_ResetBits (GPIOA, GPIO_Pin_4);
            //relay_ctrl(LEFT_RELAY, RELAY_OFF);
            //relay_ctrl(RIGHT_RELAY, RELAY_ON);
            delay_ms (1000);

            //调试时建议打开下面的error打印日志
            Can_Erros_status(CAN->ESR);

/*
  * 下面打开时为主，发送模式
* 屏蔽掉即为从模式，测试demo。
* */
#if 1
            SYS_DEBUG(("irq:0x%x",__NVIC_GetPendingIRQ(CEC_CAN_IRQn)));
            TxMessage.Data[0] = 'w';
            TxMessage.Data[1] = 'd';
            mbox = CAN_Transmit(CAN, &TxMessage);
            SYS_DEBUG(("mailbox:%d",mbox));
            delay_ms(2000);

            int i = 0;
            while((CAN_TransmitStatus(CAN, mbox) == CAN_TxStatus_Failed) && i <= 10) {
                    SYS_DEBUG(("send faild!"));
                    GPIO_SetBits (GPIOA, GPIO_Pin_4);
                    delay_ms (100);
                    GPIO_ResetBits (GPIOA, GPIO_Pin_4);
                    delay_ms (100);
                    i++;
            }
#endif

    }
}




/**
  * @}
  */


/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
